package com.chinasofti.controller;

import com.chinasofti.dao.DepartmentDao;
import com.chinasofti.service.EmployeeService;
import com.chinasofti.vo.Department;
import com.chinasofti.vo.Employee;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/register")
public class registerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //获取注册请求参数


        String employeename = request.getParameter("employeename");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        int deptid = Integer.parseInt(request.getParameter("deptid"));
        // (String employeename, String username, String phone, String email,
        // Integer departmentid, String password, String status, String role)
        //封装成Employee对象

        Employee employee = new Employee(employeename,username,phone,email,deptid,password,"0","2");
        //调用regist方法进行注册


        EmployeeService service = new EmployeeService();
        int flag = service.register(employee);
        //成功  --      失败  ---

        if(flag == 1){
            request.setAttribute("msg","注册成功正在审核!");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }else {
            request.setAttribute("msg","用户名已经存在，请重新注册!");
            //将部门信息带入
            DepartmentDao dao = new DepartmentDao();
            List<Department> departmentList = dao.selectAll();
            request.setAttribute("departmentList",departmentList);
            request.getRequestDispatcher("register.jsp").forward(request,response);
        }

    }
}
